﻿using System;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// UI层级
/// </summary>
public enum E_UI_Layer
{
    Bot,
    Mid,
    Top,
    System
}

/// 需求: 
/// Canvas: Resources/UIPrefabs/Canvas
/// other Panel: Resources/UIPrefabs/

/// <summary>
/// 功能:
/// 1.管理所有面板
/// 2.提供给外部 显示与隐藏
/// </summary>
public class UIManager : Singleton<UIManager>
{
    private string panelPath { get; } = "UIPrefabs/";

    private Dictionary<string, UIBase> panelsDic = new Dictionary<string, UIBase>();

    // Canvas下的内在层级
    private Transform bot;
    private Transform mid;
    private Transform top;
    private Transform system;

    public UIManager()
    {
        GameObject canvas = ResourcesManager.Instance.Load<GameObject>($"{panelPath}Canvas");
        GameObject.DontDestroyOnLoad(canvas);

        bot = canvas.transform.Find("Bot");
        mid = canvas.transform.Find("Mid");
        top = canvas.transform.Find("Top");
        system = canvas.transform.Find("System");
    }

    public void ShowPanel<T>(string panelName, E_UI_Layer uiLayer, Action<T> callback = null) where T : UIBase
    {
        if (panelsDic.ContainsKey(panelName))
        {
            //已经显示了面板
            panelsDic[panelName].Show();
            callback?.Invoke(panelsDic[panelName] as T);
            return;
        }

        ResourcesManager.Instance.LoadAsync<GameObject>(panelPath + panelName, (panel) =>
        {
            // 面板父物体
            Transform parent = null;
            switch (uiLayer)
            {
                case E_UI_Layer.Bot:
                    parent = bot;
                    break;
                case E_UI_Layer.Mid:
                    parent = mid;
                    break;
                case E_UI_Layer.Top:
                    parent = top;
                    break;
                case E_UI_Layer.System:
                    parent = system;
                    break;
            }
            if(panel == null)
            {
                throw new Exception($"Can not found: '{panelPath+panelName}' Check you Assets or program!");
            }
            panel.transform.SetParent(parent);
            // 面板位置
            panel.transform.localPosition = Vector3.zero;
            panel.transform.localScale = Vector3.one;
            (panel.transform as RectTransform).offsetMax = Vector2.zero;
            (panel.transform as RectTransform).offsetMin = Vector2.zero;
            // 获取面板上的脚本 如果没有就新创建一个
            T ui = panel.GetComponent<T>();
            if (ui == null)
                ui = panel.AddComponent<T>();
            // 面板实例化之后回调
            callback?.Invoke(ui);
            // 激活ui脚本
            ui.Show();

            // 存入列表
            panelsDic.Add(panelName, ui);
        });
    }

    /// <summary>
    /// 隐藏(删除面板)
    /// 调用面板的Hide方法 后 延迟t秒Destroy
    /// </summary>
    /// <param name="panelName">面板名</param>
    /// <param name="t">延迟时间</param>
    public void HidePanel(string panelName, float t = 0f)
    {
        if (panelsDic.TryGetValue(panelName, out UIBase ui))
        {
            ui.Hide();
            GameObject.Destroy(ui.gameObject, t);
            panelsDic.Remove(panelName);
        }
    }
}
